﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using T.ApiResults;
using T.Materials.Domain.Dto;
using T.Materials.Domain.Model;
using T.Materials.Read.API.ApplicationRead.Command.MaterialRequisitionCommand;
using T.Materials.Read.API.ApplicationRead.Command.MaterialStatisticsCommand;
using T.Materials.Read.API.ApplicationRead.Command.MaterialStoreCommand;

namespace T.Materials.Read.API.Controllers
{
    /// <summary>
    /// API控制器--显示物料统计信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ShowMaterialStatisticsController : ControllerBase
    {
        /// <summary>
        /// 命令总线
        /// </summary>
        protected readonly IMediator mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">命令总线</param>
        public ShowMaterialStatisticsController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 查物物资统计资列表
        /// </summary>
        /// <param name="command">查询物统计库命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpGet]
        public async Task<ApiResult<ListMaterialStoreDto<MaterialStoreDto>>> GetMaterialStatisticsList([FromQuery] ShowMaterialStatisticsCommand command)
        {
            ApiResult<ListMaterialStoreDto<MaterialStoreDto>> result = new ApiResult<ListMaterialStoreDto<MaterialStoreDto>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "查物物资统计资列表失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填物资统计
        /// </summary>
        /// <param name="command">反填物资统计命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpGet]
        public async Task<ApiResult<MaterialStatistics>> ReverseMaterialStatistics([FromQuery] ReverseMaterialStatisticsCommand command)
        {
            ApiResult<MaterialStatistics> result = new ApiResult<MaterialStatistics>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "新增物资统计失败";
                throw;
            }
            return result;
        }
    }
}
